package study.远程调用;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.util.ObjectUtils;
import study.远程调用.entity.AcquisitionPoint;
import study.远程调用.entity.Room;
import study.远程调用.enums.DeviceTypeEnum;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author 刘伟涛
 * @Date 2024/6/14 13:47
 **/
public class HttpUtils {
    public static void main(String[] args) {

        String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxfDE3MTg2OTc2MDIzNzAiLCJpYXQiOjE3MTg2OTc2MDIsImV4cCI6MTcxOTU2MTYwMn0.j3llWlP_6zDMzoHdkl1CQ7QKg3rUdtmNJ5XfYYtLJRcU3fYZyVQmI9iKyOCw6g5CVIWQfpX_CNsZ_5mHtuCNcg";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("thingKey", "001000000000");
        String result = HttpRequest.post("http://218.17.50.150:6610/biz/equipmentPoint/getPointListByEquipmentIds")
                .header("token", token)
                .form(paramMap)
                .timeout(20000)//超时，毫秒
                .execute().body();
        System.out.println(result);

        if(!(JSONUtil.parseObj(result) instanceof List)){
            return;

        }
        //获取采集点信息
        List<AcquisitionPoint> acquisitionPoints = JSONUtil.toList(JSONUtil.parseArray(result), AcquisitionPoint.class);
        List<String> collect = acquisitionPoints.stream().map(AcquisitionPoint::getGatherKey).collect(Collectors.toList());
        String s = JSONUtil.toJsonStr(collect);
//        System.out.println(collect);
//        System.out.println("JSON:" + s);
        // key：gatherKey，value:房间ID+设备类型
        Map<String, String> gatherKeyMap = acquisitionPoints.stream().peek(acquisitionPoint -> {
            String name = acquisitionPoint.getName();
            // 使用正则表达式匹配 '-' 或者 空格
            String[] parts = name.split("[- ]");
//            acquisitionPoint.setName(parts[parts.length - 1] + name.charAt(0));
            acquisitionPoint.setName(parts[parts.length - 1] + name.charAt(0) + name.charAt(1) +name.charAt(2));
        }).collect(Collectors.toMap(AcquisitionPoint::getGatherKey, v1 -> v1.getEquipmentInfoId() + v1.getName()));

        //获取实时数据
        HashMap<String, Object> paramMap1 = new HashMap<>();
        paramMap1.put("points", s);
        paramMap1.put("isfindDB", "true");
        String result1 = HttpRequest.post("http://218.17.50.150:6610/biz/findData/getRealData")
                .header("token", token)
                .form(paramMap1)
                .timeout(20000)//超时，毫秒
                .execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(result1);
//        System.out.println("json:"+jsonObject);

        Map<String, String> resultMap = new HashMap<>();
        int num = 0;
        for (String s1 : gatherKeyMap.keySet()) {
            String key = gatherKeyMap.get(s1);
            System.out.println("gatherKey："+s1+"key：" + key+"value：" + jsonObject.getStr(s1));
            if(key.contains("设备状态P")){
            num++;
            }
            if(!ObjectUtils.isEmpty(resultMap.get(key))){
                System.out.println(key);
            }
            resultMap.put(key, jsonObject.getStr(s1));
        }
        System.out.println(num);

        Set<String> strings = resultMap.keySet();
        List<String> strings1 = strings.stream().filter(s1->s1.contains("设备状态P")).collect(Collectors.toList());
        System.out.println(strings1.size());
        List<String> strings2 = strings1.stream().map(s2 -> s2.split("P")[1].split("value")[0]).sorted().collect(Collectors.toList());
        for (String s1 : strings2) {
            System.out.println(s1);
        }

        System.out.println("总计数据条数：" + gatherKeyMap.size());
        System.out.println("总结房间数：" + getRoomSize(token));
        System.out.println(resultMap.get(17+ DeviceTypeEnum.DEVICE_STATUS_T.getName()));

    }

    private static List<Room> getRoomInfo(String token){
        //获取实时数据
        HashMap<String, Object> paramMap1 = new HashMap<>();
        paramMap1.put("thingKey", "001000000000");
//        paramMap1.put("isfindDB", "true");
        String result = HttpRequest.post("http://218.17.50.150:6610/biz/equipmentPoint/findEquipmentListByTreeId")
                .header("token", token)
                .form(paramMap1)
                .timeout(20000)//超时，毫秒
                .execute().body();
        return JSONUtil.toList(JSONUtil.parseArray(result), Room.class);
    }

    private static int getRoomSize(String token){
        return getRoomInfo(token).size();
    }
}
